<template>
  <section>
    <el-row style="display: flex;align-items: center;">
      <el-col :span="6">
        <el-input size="mini" placeholder="请输入关键字"  clearable v-model="searchKey" @keydown.native.prevent.enter="searchBtn" ></el-input>
      </el-col>
      <el-col :span="4">
        <i class="el-icon-search" @click="searchBtn" style="margin-left:10px" ></i>
      </el-col>
      <el-col :span="12" >
        <el-button size="mini" @click="addLevel" style="float: right" type="primary">新建属性等级</el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-table :data="levelList" stripe v-loading="loading">
        <el-table-column type="index" label="序号"  width="80px"></el-table-column>
        <el-table-column prop="name" label="名称" width="150px">
          <template slot-scope="scope">
            <el-input v-show="scope.row.edit" size="small" v-model="scope.row.name"></el-input>
            <span  v-show="!scope.row.edit">{{scope.row.name}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="code" label="编码" width="200px">
          <template slot-scope="scope">
            <el-input v-show="scope.row.edit" size="small" v-model="scope.row.code"></el-input>
            <span  v-show="!scope.row.edit">{{scope.row.code}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="min" label="起始值" width="160px">
          <template slot-scope="scope">
            <el-input v-show="scope.row.edit" size="small" v-model="scope.row.min"></el-input>
            <span  v-show="!scope.row.edit">{{scope.row.min}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="max" label="结束值">
          <template slot-scope="scope">
            <el-input v-show="scope.row.edit" size="small" v-model="scope.row.max"></el-input>
            <span  v-show="!scope.row.edit">{{scope.row.max}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" fixed="right">
          <template slot-scope="scope">
            <el-button :type="scope.row.edit?'success':'primary'" @click='confirm(scope.row)' size="small" icon="edit">{{scope.row.edit?'完成':'编辑'}}</el-button>
            <el-button @click='cancelBtn(scope.row)' size="small" v-if="scope.row.edit">取消</el-button>
            <el-button @click='del(scope)' size="small" v-if="!scope.row.edit">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
  </section>
</template>

<script>
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  import Vue from 'vue'//后续添加属性，需要vue联动
  import errHandler from '@/assets/errHandler'
  export default {
    name: "activityPropertiesLevel",
    props: [''],
    data() {
      return {
        loading:false,
        searchKey:null,
        levelList:null
      }
    },
    created(){
      console.log('等级页面')
      this.getActivityPropertiesLevelList()
    },
    computed:{
    },
    watch: {
    },
    methods: {
      ...mapActions({
        fetch: 'fetch'
      }),
      getActivityPropertiesLevelList(){
        this.fetch({name:"getActivityPropertiesLevelList"})
          .then(res=>{
            console.log(res)
            this.levelList=res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      searchBtn(){
        console.log('查询一下')
      },
      addLevel(){
        //新建一个空的level，表格页面+1栏
        this.fetch({name:"createActivityPropertiesLevel",data:{name:'',min:'',max:''}})
          .then(res=>{
            console.log(res)
            res.edit = true
            this.levelList.push(res)
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      confirm(row){
        console.log(row)
        if(row.edit === undefined){
          Vue.set(row,'edit',true)//后续添加属性，需要vue联动
          return
        }
        if(row.edit){
          this.loading=true
          this.updateActivityPropertiesLevel(row)
        }else {
          row.edit=true
        }
      },
      cancelBtn(row){//取消按钮
        if(row.edit === undefined){
          Vue.set(row,'edit',false)//后续添加属性，需要vue联动
        }else {
          row.edit =false
        }
      },
      del(scope){//删除按钮
        console.log(scope)
        this.loading=true
        this.$confirm('确认删除？')
          .then(()=> {
            this.fetch({name:"deleteActivityPropertiesLevel",data:{id:scope.row.id}})
              .then(res=>{
                this.loading=false
                this.levelList.splice(scope.$index,1)
              })
              .catch(err=>{
                console.log(err)
                errHandler.handle(this,err)
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
            this.loading = false
          })
      },
      updateActivityPropertiesLevel(row){
        this.fetch({name:"updateActivityPropertiesLevel",data:{...row}})
          .then(res=>{
            this.loading=false
            console.log(res)
            row.edit=false
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      }
    }
  }
</script>

<style scoped>

</style>
